{% extends "base.html" %}

{% block css %}
<link rel="stylesheet" type="text/css" href="/statics/css/booking.css" />
{% endblock %}


{% block javascript %}
<script type="text/javascript" src="/statics/javascript/booking.js"></script>
<script type="text/javascript">

    function refleshUser(){
        $.get("/user/list",{},  function(data){
              $('#user_list').children().remove();
              $('#user_list').html(data);
         }, 'html');
    }
    
    function deleteUser(key){
        if(key != null){
            $.post("/user/del/" + key, {'_method':'delete'},
                function(data) {
                    var data = $.xml2json(data);
                    if(data.error){
                        showMessage(data.error);
                    } else {
                        showMessage(data.success);
                        refleshUser();
                    }
                }, 'xml');
            
        }
    }
    
    function makeUserChairman(key){
        if(key != null){
            $.post("/user",
                {'user_key' : key},
                function(data){
                    var data = $.xml2json(data);
                    if(data.error){
                        showMessage(data.error);
                    } else {
                        showMessage(data.success);
                        refleshUser();
                    }                
                },'xml');
        }
    }

    $(function(){
        
        $('.btn_del_user').live('click', function(){
            var btn_id = $(this).attr('id');
            var user_key = btn_id.substring(13);
            deleteUser(user_key);
        });
        
        $('.btn_charge_user').live('click', function(){
            var btn_id = $(this).attr('id');
            var user_key = btn_id.substring(16);
            makeUserChairman(user_key);
        });

    });
</script>
{% endblock %}

{% block title %}用户维护{% endblock %}

{% block content %}
<h1>用户</h1>
<table clas="dish_table" border="1" style="border-collapse: collapse">
<thead>
<tr>
    <th style="width:150px">编号</th>
    <th style="width:150px">姓名</th>
    <th style="width:150px">操作</th>
    <th style="width:150px">当天轮值</th>
</tr>
</thead>
<tbody id="user_list">
    {% if users %}
	    {% for user in users %}
	    <tr align="center">        
	        <td width="50">{{ user.code }}</td>
	        <td width="100">{{ user.name }}</td>
	        <td width="100"><button id="btn_del_user_{{ user.key }}" class="btn_del_user">删除</button></td>
	        <td width="100">
                {% if chairmanKey %}
	                {% ifequal user.key chairmanKey %}
	                    <a href="#" onclick="makeUserChairman('{{ user.key }}')">我不想收钱</a>
	                {% endifequal %}
	                {% ifnotequal user.key chairmanKey %}
                        <button id="btn_charge_user_{{ user.key }}" class="btn_charge_user">设为收钱者</button>
                    {% endifnotequal %}
	            {% else %}
	                <button id="btn_charge_user_{{ user.key }}" class="btn_charge_user">设为收钱者</button>
	            {% endif %}
            </td>
	    </tr>
	    {% endfor %}
    {% endif %}
</tbody>
</table>
{% endblock %}